<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ include file="/include/taglibs.jsp" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page isELIgnored="false" %>
<head>
    <title>用户数据权限管理</title>
    <link rel="stylesheet" href="${ctx}/assets/admin/LingUI/css/ui.jqgrid.css"/>
</head>

<div class="row">
    <div class="col-xs-12">
        <%--查询条件更多 --%>
        <div class="b_grid_line  col-lg-12"></div>                        <!-- 分隔线 -->
        <table id="module_list_table" class="grid-table"></table>
        <%--jqgrid数据表格 --%>
        <div id="module_list_pager"></div>
        <%--jqgrid表格 页脚 --%>
    </div>
</div>
<script type="text/javascript">
    var scripts = [null,
        "${ctx}/assets/admin/LingUI/js/jqGrid/jquery.jqGrid.js",
        "${ctx}/assets/admin/LingUI/js/jqGrid/i18n/grid.locale-cn.js",
        "${ctx}/assets/admin/LingUI/js/control.js",
        "${ctx}/assets/admin/LingUI/js/jsutil/js/jquery.filterSearch.js",   //筛选搜索
        null];
    var _grid_id = "#module_list_table";	//表格 选择器
    var _pager_id = "#module_list_pager";	//表格页脚 选择器
    ace.load_ajax_scripts(scripts, function () {

        //调整以适应页面大小  其中 $(".page-content") 为jqgrid的父级容器
        $(window).off('resize.jqGrid').on('resize.jqGrid', function () {
            setTimeout(function () {
                $(_grid_id).jqGrid("setGridWidth", $(".page-content").width());
                $(".ui-jqgrid-bdiv").width($(".page-content").width() + 1);
            }, 50);
        });
        jQuery(_grid_id).jqGrid({
            mtype: "POST",
            url: "${ctx}/${frameworkPath}/resource-permission/list?sort=USER_ID&&dir=asc&&USER_ID=${param.userId}",
            colNames: ['编号', '权限类型', '资源数据类型','permissionTypeId','resourceTypeId', '拥有人', '用户集合', '部门集合', '操作'],
            colModel: [
                {name: 'id', index: 'id', width: 0, editable: true, hidden: true},
                {name: 'permissionType', index: 'permissionType', width: 0, editable: true},
                {name: 'resourceType', index: 'resourceType', width: 60, editable: true},
                {name: 'permissionTypeId', index: 'permissionTypeId', width: 0, editable: true, hidden:true},
                {name: 'resourceTypeId', index: 'resourceTypeId', width: 0, editable: true, hidden:true},
                {name: 'userId', index: 'userId', width: 60, editable: true, align: 'center'},
                {name: 'userIds', index: 'userIds', width: 0, editable: true, align: 'center'},
                {name: 'orgIds', index: 'orgIds', width: 0, editable: true, align: 'center'},
                {
                    name: 'action',
                    index: '',
                    width: 100,
                    fixed: false,
                    sortable: false,
                    align: 'center',
                    formatter: function (cellvalue, options, rowObject) {
                        var s = '<div class=" action-buttons">'
                        /*s = s + '<a class="blue my-tooltip-link tooltip-primary" title="编辑" ' +
                                'style="cursor:pointer" ><i class="ace-icon fa fa-pencil bigger-130 " ' +
                                'onclick="editrow(\'' + rowObject.userId + '\',\'' + rowObject.resourceTypeId + '\',\'' + rowObject.permissionTypeId + '\')"></i></a>';*/
                        s = s + '<a class="red my-tooltip-link tooltip-primary" title="删除" style="cursor:pointer"><i class="ace-icon fa fa-trash-o bigger-130"  onclick="deleterows(\'' + rowObject.id + '\')"></i></a>';
                        return s + '</div>';
                    }
                }
            ],
            pager: _pager_id, //分页信息 放置的位置
            page: lui.pageNum(_grid_id)
        });
        //自定义工具栏 =============BEGIN======================//
        jQuery(_grid_id)
                .jqGrid('navGrid', _pager_id)
                .navButtonAdd(_pager_id, {
                    title: "删除",
                    buttonicon: "ace-icon fa fa-trash-o red",
                    onClickButton: deleteRows
                })
                .navButtonAdd(_pager_id, {
                    title: "刷新",
                    buttonicon: "ace-icon fa fa-refresh orange",
                    onClickButton: function () {
                        //callback();
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                })

    });

    //编辑数据
    function editrow(userId, resourceType, permissionType) {
        lui.open({
            title: '编辑数据权限',
            content: "${ctx}/${frameworkPath}/resource-permission/edit?userId=" + $.trim(userId) + "&resourceType=" + $.trim(resourceType) + "&permissionType=" + $.trim(permissionType),
            area: ["50%", "450px"],
            yes: function (index, layero) {
                var iframeWin = $(layero).find("iframe")[0].contentWindow;
                var retObj = iframeWin.formValid();
                if (retObj.text) {
                    lui.info(retObj.text);
                    if (retObj.state) {
                        layer.close(index);
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                }
            }
        });
    }

    //删除
    function deleterows(id) {
        lui.confirm('确实要删除吗？', function () {
            var idArray = new Array();
            idArray.push(id);
            $.ajax({
                type: 'POST',
                url: '${ctx}/${frameworkPath}/resource-permission/delete',
                traditional: true,
                dataType: 'json',
                data: {'ids': idArray},
                success: function (data) {
                    if (data.success) {
                        lui.success('删除成功');
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                    else {
                        lui.fail('删除失败');
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    lui.fail('删除失败');
                }
            });
        }, function () {
        });
    }

    function deleteRows() {
        var ids = $("#module_list_table").jqGrid('getGridParam', 'selarrrow');
        if (ids.length < 1) {
            bootbox.alert({title: '提示消息', message: '至少选择一行数据'});
            return;
        }
        lui.confirm('确实要删除吗？', function () {
            var idArray = new Array();
            for (var i = 0; i < ids.length; i++) {
                idArray.push(ids[i]);
            }
            $.ajax({
                type: 'POST',
                url: '${ctx}/${frameworkPath}/resource-permission/delete',
                traditional: true,
                dataType: 'json',
                data: {'ids': idArray},
                success: function (data) {
                    if (data.success) {
                        lui.success('删除成功');
                        jQuery(_grid_id).trigger("reloadGrid");
                    }
                    else {
                        lui.fail('删除失败');
                    }
                },
                error: function (XmlHttpRequest, textStatus, errorThrown) {
                    lui.fail('删除失败');
                }
            });
        }, function () {
        });
    }
</script>